package com.lkq.mongodb_signin.controller;

import com.lkq.mongodb_signin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public String index(Model model){
        model.addAttribute("state",userService.list());
        return "index";
    }
    @GetMapping("/signin")
    public String signin(String name, Model model){
        if (! userService.update(name)){
            return "index";
        }
        model.addAttribute("state",userService.list());
        return "index";
    }

    @PostMapping("/signin1")
    public String load(MultipartFile file, Model model){
        String fileName = file.getOriginalFilename();
        try {
            userService.batchImport(fileName,file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("state",userService.list());
        return "index";
    }


}
